<html>
<head><meta charset="utf-8"><title>at_exit ? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html">at_exit ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219667136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219667136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219667136">(Dec 11 2020 at 21:52)</a>:</h4>
<p>Is there interest in adding an <code>at_exit</code> function to the standard library? It would be really helpful for e.g. <a href="#narrow/stream/122651-general/topic/DHAT.20and.20rustdoc/near/219666747">ensuring a memory profiler always runs even if not dropped</a>, since it's meant to live for the lifetime of the program and often <code>process::exit</code> is called before it's dropped</p>



<a name="219667157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219667157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219667157">(Dec 11 2020 at 21:52)</a>:</h4>
<p>there's already a private <code>sys_common::at_exit</code> but I don't think it's stable</p>



<a name="219667197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219667197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219667197">(Dec 11 2020 at 21:53)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120989">@njn</span></p>



<a name="219671193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219671193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219671193">(Dec 11 2020 at 22:40)</a>:</h4>
<p>at_exit is extremely unsafe to use</p>



<a name="219671270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219671270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219671270">(Dec 11 2020 at 22:40)</a>:</h4>
<p>you can only invoke async-signal-safe operations inside of it</p>



<a name="219671298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219671298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219671298">(Dec 11 2020 at 22:40)</a>:</h4>
<p>and it behaves very differently on unix and windows</p>



<a name="219680140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219680140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219680140">(Dec 12 2020 at 00:54)</a>:</h4>
<p>Code also continues to run in other threads after those callbacks have executed</p>



<a name="219680167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219680167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219680167">(Dec 12 2020 at 00:54)</a>:</h4>
<p>unless you're on windows, where all threads but the main thread are abruptly killed in the middle of whatever they were doing!</p>



<a name="219680189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219680189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219680189">(Dec 12 2020 at 00:55)</a>:</h4>
<p>whoa that's news to me</p>



<a name="219680348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219680348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219680348">(Dec 12 2020 at 00:58)</a>:</h4>
<p>That's why we had to get rid of log's atexit callback iirc</p>



<a name="219680358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219680358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219680358">(Dec 12 2020 at 00:58)</a>:</h4>
<p>It would just deadlock on windows if the process exited while a thread was installing a logger</p>



<a name="219682936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219682936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219682936">(Dec 12 2020 at 01:56)</a>:</h4>
<p>oh that is alarming</p>



<a name="219682944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219682944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219682944">(Dec 12 2020 at 01:57)</a>:</h4>
<p>I guess it could be <code>unsafe fn</code>? but it sounds like all the invariants aren't known and it might just crash</p>



<a name="219829640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219829640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219829640">(Dec 14 2020 at 11:44)</a>:</h4>
<p>both rust's std and C's stdlib have an 'at exit' function. rust's one is pretty safe. it's directly called from process::exit.</p>



<a name="219829799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219829799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219829799">(Dec 14 2020 at 11:46)</a>:</h4>
<p>(but that one doesn't work for Rust libraries used from C programs. it's used for flushing stdout at exit and WSACleanup on windows)</p>



<a name="219845863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219845863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219845863">(Dec 14 2020 at 14:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="310399">Mara</span> <a href="#narrow/stream/219381-t-libs/topic/at_exit.20.3F/near/219829799">said</a>:</p>
<blockquote>
<p>(but that one doesn't work for Rust libraries used from C programs. it's used for flushing stdout at exit and WSACleanup on windows)</p>
</blockquote>
<p>FWIW, that would work fine for my use case, which is a Rust binary with a global allocator whose destructor isn't running</p>



<a name="219845931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219845931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219845931">(Dec 14 2020 at 14:14)</a>:</h4>
<p>(because someone used <code>process::exit</code>, not because globals aren't dropped, it's a local in <code>main</code>)</p>



<a name="219848915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219848915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219848915">(Dec 14 2020 at 14:34)</a>:</h4>
<p>that one has a pretty good implementation. it even handles adding more at_exit handlers from within such a handler, with a recursion limit etc. (which is overkill considering it's used only in two places now)</p>
<p>so exposing that one is probably fine.</p>



<a name="219899289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219899289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219899289">(Dec 14 2020 at 20:43)</a>:</h4>
<p>We could call it <code>at_rust_main_exit</code> or similar to convay that's on the exit of rust main and not on process exit.</p>
<p>For the case rust is integrated into C we could provide a <code>unsafe</code> way to call that exit handler(s), including in the documentation the problems with C at_exit method and appropriate recommendations about how/what to call it.</p>



<a name="219901218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219901218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219901218">(Dec 14 2020 at 21:00)</a>:</h4>
<p>But process::exit works either way and will Invoke rust handlers regardless of whether main was rust controlled</p>



<a name="219912053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219912053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219912053">(Dec 14 2020 at 22:33)</a>:</h4>
<p>it runs after a rusty <code>main</code> or calls to rust <code>std::process::exit</code>, but not any external cause like <code>libc::exit</code></p>



<a name="219915329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219915329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219915329">(Dec 14 2020 at 23:07)</a>:</h4>
<p>It also won't run if you exit via signal. But most importantly, it won't run in the library case. And honestly, the most <em>common</em> times I see C programmers use <code>atexit</code> is in libraries.</p>



<a name="219915734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/at_exit%20%3F/near/219915734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/at_exit.20.3F.html#219915734">(Dec 14 2020 at 23:11)</a>:</h4>
<p>often incorrectly! <a href="https://github.com/openssl/openssl/issues/6214">https://github.com/openssl/openssl/issues/6214</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>